package com.jxgm.spring.boot.service;

import com.jxgm.spring.boot.entity.Department;
import com.jxgm.spring.boot.entity.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * @date 2020/10/14 10:50
 * @description
 **/
@Repository
public class EmpService {

    private static Map<Integer, Emp> empMap = null;

    @Autowired
    private DepartmentService departmentService;

    static {

        empMap = new HashMap<Integer, Emp>();

        empMap.put(1001,new Emp(1001, "E-AA", "aa@163.com", 1, new Department(101, "D-AA")));
        empMap.put(1002,new Emp(1002, "E-BB", "bb@163.com", 0, new Department(101, "D-BB")));
        empMap.put(1003,new Emp(1003, "E-CC", "cc@163.com", 1, new Department(101, "D-CC")));
        empMap.put(1004,new Emp(1004, "E-DD", "dd@163.com", 1, new Department(101, "D-DD")));
        empMap.put(1005,new Emp(1005, "E-EE", "ee@163.com", 0, new Department(101, "D-EE")));

    }

    private static Integer initId = 1006;

    public void saveEmp(Emp emp){

       if (emp.getEmpId()==null){
           emp.setEmpId(initId++);
       }

       emp.setDepartment(departmentService.getDepartment(emp.getDepartment().getDno()));

       empMap.put(emp.getEmpId(),emp);

    }

    public Collection<Emp> getEmps(){

        return empMap.values();

    }

    public Emp getId(Integer id){
        return empMap.get(id);
    }

    public Emp removeId(Integer id){
        return empMap.remove(id);
    }


}
